bin/set-origin: Don't crash if origin has no refspec
authorColin Walters <walters@verbum.org>
Tue, 29 Apr 2025 22:31:16 +0000 (18:31 -0400)
committerColin Walters <walters@verbum.org>
Tue, 29 Apr 2025 22:33:29 +0000 (18:33 -0400)
e.g. if it's using a container instead.

Signed-off-by: Colin Walters <walters@verbum.org>
src/ostree/ot-admin-builtin-set-origin.c

index 1bb0d098f48a5ca651a257963331d83702ec9991..d014fb001499e6cf5ccc07106d204e5e929d7cbe 100644 (file)
@@ -111,12 +111,18 @@ ot_admin_builtin_set_origin (int argc, char **argv, OstreeCommandInvocation *inv
     g_autofree char *origin_remote = NULL;
     g_autofree char *origin_ref = NULL;
 
-    if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
-      return FALSE;
+    if (origin_refspec != NULL)
+      {
+        if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
+          return FALSE;
+      }
+    else if (branch == NULL)
+      return glnx_throw (error, "No host refspec found, branch is required");
 
+    const char *target_branch = branch ?: origin_ref;
+    g_assert (target_branch);
     {
-      g_autofree char *new_refspec
-          = g_strconcat (remotename, ":", branch ? branch : origin_ref, NULL);
+      g_autofree char *new_refspec = g_strconcat (remotename, ":", target_branch, NULL);
       g_autoptr (GKeyFile) new_origin = NULL;
 
       new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec);